#include "MyLinkList.h"

template<class T>
MyLinkList<T> MyLinkList<T>::Init_LinkList() {
    MyLinkNode<T>* node = new MyLinkNode<T>();
    node->data = NULL;
    node->next = NULL;
    this->head = node;
    this->size = 0;
    return *this;
}

template<class T>
void MyLinkList<T>::Insert_LinkList(int pos, T* data) {
    if(this == NULL) return;
    if(data == NULL) return;

    MyLinkNode<T>* newNode = new MyLinkNode<T>();
    newNode->data = data;
    newNode->next = NULL;

    //插入，找到一个指向pos-1的结点来的辅助指针
    MyLinkNode<T>* pCurrent = this->head;
    for(int i = 1;i <= pos - 1;i++) {
        pCurrent = pCurrent->next;
    }
    //循环过后，此时的辅助结点就是插入结点的前一个
    newNode->next = pCurrent->next;
    pCurrent->next = newNode;

    this->size++;
}

template<class T>
MyLinkList<T> MyLinkList<T>::List_HeadInsert(int size) {
    //用户输入size个整数
    int x;
    scanf("%d\n",&x);
    MyLinkNode<T>* newNode = new MyLinkNode<T>();

    for(int i = 0;i < size;i++) {
        //初始化一个结点
        MyLinkNode<T>* newNode = new MyLinkNode<T>();
        scanf("%d\n",&x);
        newNode->data = &x;
        newNode->next = this->head->next;
        this->head->next = newNode;
        this->size++;
    }
    return *this;
}

template<class T>
MyLinkList<T> MyLinkList<T>::List_TailInsert(int size) {
    //用户输入size个整数
    int x;
    scanf("%d\n",&x);
    MyLinkNode<T>* newNode = new MyLinkNode<T>();

    //找结点
    //辅助指针变量
    MyLinkNode<T>* pCurrent = this->head;
    for(int i = 0; i < size;i++) {
        pCurrent = pCurrent->next;
    }

    for(int i = 0;i < size;i++) {
        //初始化一个结点
        MyLinkNode<T>* newNode = new MyLinkNode<T>;
        scanf("%d\n",&x);
        newNode->data = &x;
        newNode->next = NULL;

        //遍历完后，辅助指针就指向的是最后一个结点
        pCurrent->next = newNode;
        pCurrent = pCurrent->next;
        this->size++;
    }
    return *this;
}

template<class T>
MyLinkNode<T> MyLinkList<T>::getElem(int pos) {
    if(pos < 1 || pos >= this->size) new MyLinkNode<T>();

    MyLinkNode<T>* pCurrent = this->head;
    //辅助指针
    for(int i = 1;i <= pos;i++) {
        pCurrent = pCurrent->next;
    }
    return *pCurrent;
}

template<class T>
int MyLinkList<T>::Find_LinkList(T data) {
    if(data == NULL) return -1;
    //定义辅助指针变量
    MyLinkNode<T>* pCurrent = this->head->next;
    for(int i = 1;i <= this->size;i++) {
        if(pCurrent->data == data) {
            return i;
        }
        pCurrent = pCurrent->next;
    }
    return 0;
}

template<class T>
void MyLinkList<T>::RemoveByPos_LinkList(int pos) {
    if(this == NULL) return;
    if(pos < 1 || pos > this->size) return;

    //找结点
    //辅助指针变量
    MyLinkNode<T>* pCurrent = this->head;
    //指向前一个结点
    for(int i = 1; i < pos;i++) {
        pCurrent = pCurrent->next;
    }

    //缓存删除的结点
    MyLinkNode<T>* pDel = pCurrent->next;
    pCurrent->next = pDel->next;
    //释放删除结点的内存
    delete pDel;

    this->size--;
}

template<class T>
int MyLinkList<T>::Size_LinkList() {
    if(this == NULL) return -1;
    return this->size;
}

template<class T>
void MyLinkList<T>::FreeSpace_LinkList() {
    //辅助指针变量
    MyLinkNode<T>* pCurrent = this->head;

    for(int i = 0;i <= this->size;i++) {
        //缓存下一个结点
        MyLinkNode<T>* pNext = pCurrent->next;
        delete pNext;
        pCurrent = pNext;
    }

    //释放链表内存
    this->head = NULL;
    this->size = 0;

    delete this;
}


template<class T>
void MyLinkList<T>::Print_LinkList(PRINTLINKNODE print) {
    if(this == NULL) return;
    //定义辅助指针变量
    MyLinkNode<T>* pCurrent = this->head->next;
    for(int i = 0;i < this->size;i++) {
        print(pCurrent->data);
        pCurrent = pCurrent->next;
    }
}


